package org.android.bbox.data;

import org.android.bbox.activities.ProfilChoice;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class Profil
{
	private String ip;
	private String name;
	private int id;
	private String login;
	private String password;
	private String password_user;
	private boolean save_password;
	private String image;
	private String color;

	private ContentValues update;

	private DbHelper db;

	public static int create(Context context, String ip, String name, String login, String password, String passwordUser, boolean savePassword, String image, String color) throws NoDatabaseException
	{
		ContentValues values = new ContentValues();
		values.put("ip", ip);
		values.put("name", name);
		values.put("login", login);
		values.put("password", password);
		values.put("password_user", passwordUser);
		values.put("save_password", savePassword);
		values.put("image", image);
		values.put("color", color);

		Profil p = new Profil(context);

		return (int)p.db.getDatabase().insertOrThrow(DbHelper.TABLE_PROFILS, "", values);
	}

	public static Cursor getAll(Context context)
	{
		Cursor c = null;
		try
		{
			Profil p = new Profil(context);
			c = p.db.getDatabase().query(DbHelper.TABLE_PROFILS, new String[]{"_id, name, image, color"}, null, null, null, null, "name");
		}
		catch (NoDatabaseException e)
		{
			Log.e(Profil.class.toString(), "Impossible to get database access", e);
		}
		return c;
	}

	public Profil(Context context, int profilId) throws NoDatabaseException, ProfilNotFoundException
	{
		this(context);
		this.getProfilFromId(profilId);
	}

	private Profil(Context context) throws NoDatabaseException
	{
		try
		{
			this.db = new DbHelper(context);
		}
		catch (NoDatabaseException e)
		{
			Log.e("log_tag", "Can't open database");
			throw e;
		}
	}

	public int update()
	{
		int count = 0;

		if (this.update.size() > 0)
		{
			String[] selectionArgs = {"" + this.id};
			count = this.db.getDatabase().update(DbHelper.TABLE_PROFILS, this.update, "_id = ?", selectionArgs);
		}

		return count;
	}

	public int delete()
	{
		String[] selectionArgs = {"" + this.id};
		return this.db.getDatabase().delete(DbHelper.TABLE_PROFILS, "_id = ?", selectionArgs);
	}

	//
	// Getters
	//
	public int getId()
	{
		return this.id;
	}

	public String getImage()
	{
		//setImageResource
		return this.image;
	}

	public String getColor()
	{
		return this.color;
	}

	public String getIp()
	{
		return this.ip;
	}

	public String getName()
	{
		return this.name;
	}

	public String getLogin()
	{
		return this.login;
	}

	public String getPassword()
	{
		return this.password;
	}

	public String getPassword_user()
	{
		return this.password_user;
	}

	public boolean isSave_password()
	{
		return this.save_password;
	}

	//
	// Setters
	//
	public void setIp(String ip)
	{
		this.setStringKey("ip", ip);
	}

	public void setName(String name)
	{
		this.setStringKey("name", name);
	}

	public void setLogin(String login)
	{
		this.setStringKey("login", login);
	}

	public void setPassword(String password)
	{
		this.setStringKey("password", password);
	}

	public void setPassword_user(String passwordUser)
	{
		this.setStringKey("password_user", passwordUser);
	}

	public void setImage(String image)
	{
		this.setStringKey("image", image);
	}

	public void setColor(String color)
	{
		this.setStringKey("color", color);
	}

	public void setSave_password(boolean savePassword)
	{
		this.getContentValues().put("save_password", savePassword);
	}

	public String toString()
	{
		return "Profile : " + this.getName() + "(" + this.getId() + ") to access " + this.getIp();
	}

	/**
	 * Get the ContentValues for update
	 */
	private ContentValues getContentValues()
	{
		if (this.update == null)
		{
			this.update = new ContentValues();
		}
		return this.update;
	}

	private void setStringKey(String key, String value)
	{
		this.getContentValues().put(key, value);
	}

	private void getProfilFromId(int profilId) throws ProfilNotFoundException
	{
		String[] selectionArgs = {"" + profilId};

		Cursor c = this.db.getDatabase().query(DbHelper.TABLE_PROFILS, null, "_id = ?", selectionArgs, null, null, null);

		if (!c.moveToFirst() || c.getCount() <= 0)
		{
			throw new ProfilNotFoundException(profilId);
		}

		this.ip = c.getString(c.getColumnIndex("ip"));
		this.name = c.getString(c.getColumnIndex("name"));
		this.login = c.getString(c.getColumnIndex("login"));
		this.password = c.getString(c.getColumnIndex("password"));
		this.password_user = c.getString(c.getColumnIndex("password_user"));
		this.image = c.getString(c.getColumnIndex("image"));
		this.color = c.getString(c.getColumnIndex("color"));
		this.save_password = (c.getInt(c.getColumnIndex("save_password")) > 0);
		this.id = c.getInt(c.getColumnIndex("_id"));
	}
}
